/**
 * Created by lwy on 2017/6/13.
 */
var page=0;
var num;
$(function(){
    doAjax();
})

function doAjax(){
    page++;
    $.ajax({
        url:'/messagesC/getUserMessages.json',
        dataType:'json',
        type:'post',
        data:{
            pageNo:page,
            token:getItem('token'),
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                var data = data.datas.messages;
                if(page==1){
                    $('#newsBox').empty();
                    if(data.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('#newsBox'));
                    }
                }
                for(var i=0;i<data.length;i++){
                    var ifRead = data[i].ifRead;
                    var ifReadHtml;
                    if(ifRead==false){
                        ifReadHtml = '<span class="ifRead"></span>';
                    }else{
                        ifReadHtml = '';
                    }
                    $('<li class="mui-table-view-cell mui-media"><a id="'+data[i].id+'">'+
                        '<img class="mui-media-object mui-pull-left" src="../img/default-autor.png">'+ifReadHtml+
                        '<div class="mui-media-body">'+
                        '<p class="news-title">'+data[i].title+'<span class="news-time">'+data[i].createTime+'</span></p>'+
                        '<p class="mui-ellipsis">'+data[i].content+'</p>'+
                        '</div></a></li>').appendTo($('#newsBox'));
                }
                num=data.length;
            }
        }
    })
    return num;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

//点击消息详细
mui('body').on('tap','.mui-table-view-cell a',function(){
    var id = $(this).attr('id');
    console.log(id);
    $.ajax({
        url:'/messagesC/updateNotReadMessages.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            messageId:id,
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                window.location.href="personalnews-detail.html?id="+id;
            }
        }
    })
})
